package com.fire.security.controller;


import com.fire.security.core.page.TableDataInfo;
import com.fire.security.domain.Articles;
import com.fire.security.domain.Result;
import com.fire.security.service.ArticleService;
import com.fire.security.service.UserService;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/article")
public class ArticleController extends BaseController {


    @Autowired
    private ArticleService articleService;

    @Autowired
    private UserService userService;

    //分页查询
    @RequestMapping("/getArticleList")
    public TableDataInfo getArticleList(Articles articles){
        startPage();
        List<Articles> articlesList = articleService.getArticlesList(articles);
        return getDataTable(articlesList);
    }

    //添加观看量
    @PostMapping("/addViewerNumber")
    public Result addViewerNumber(Integer articleId, String userId){

        Result result1 = articleService.addNumber(articleId);
        Result result2 = userService.addRead(userId);
        boolean success1 = result1.getCode() == 200;
        boolean success2 = result2.getCode() == 200;

        if (success1 && success2) {
            return Result.ok("添加观看量和阅读量成功！");
        } else if (success1 ^ success2) { // 使用异或运算符（^）判断两者状态是否不同
            int errorCode = success1 ? 502 : 501; // 根据哪个操作成功来设置错误码
            String errorMessage = success1 ? "添加阅读量成功，添加观看量失败！" : "添加观看量成功，添加阅读量失败！";
            return Result.err(errorCode, errorMessage);
        } else {
            return Result.err(503, "添加阅读量和观看量失败！");
        }

    }

    //更新文章
    @PostMapping("/updateArticle")
    public Result updateArticle(Articles articles){
        Result result = articleService.updateArticles(articles);
        return result;
    }

    //删除文章
    @PostMapping("/deleteArticle")
    public Result deleteArticle(Integer articleId){
        Result result = articleService.deleteArticles(articleId);
        return result;
    }

    //添加文章
    @PostMapping("/addArticle")
    public Result addArticle(Articles articles){
        //执行业务
        Result result = articleService.insertArticles(articles);

        //响应
        return result;
    }


    //上传路径
    private String uploadPath = "classpath:static/img";

    //上传图片
    @CrossOrigin
    @PostMapping("/img-upload")
    public Result uploadImg(@RequestParam("file") MultipartFile file){
        log.info(String.valueOf(file));
        try {
            //拿到图片上传到的目录(类路径classes下的static/img/upload)的File对象
            File uploadDirFile = ResourceUtils.getFile(uploadPath);
            //拿到图片上传到的目录的磁盘路径
            String uploadDirPath = uploadDirFile.getAbsolutePath();
            //拿到图片保存到的磁盘路径
            String fileUploadPath = uploadDirPath + "\\" + file.getOriginalFilename();
            //保存图片
            file.transferTo(new File(fileUploadPath));
            //成功响应
            return Result.ok("图片上传成功！");
        } catch (IOException e) {
            //失败响应
            e.printStackTrace();
            return Result.err(Result.CODE_ERR_BUSINESS, "图片上传失败！");
        }
    }
}
